package com.miaopasi.securitydemo.config.security;

import com.miaopasi.securitydemo.config.security.filter.TokenFilter;
import com.miaopasi.securitydemo.config.security.filter.ValidateCodeFilter;
import com.miaopasi.securitydemo.config.security.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Security配置类，会覆盖yml配置文件的内容
 *
 * @author lixin
 */
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    final JsonSuccessHandler successHandler;
    final JsonFailureHandler failureHandler;
    final JsonLogoutSuccessHandler logoutSuccessHandler;
    final JsonAuthenticationEntryPoint authenticationEntryPoint;
    final JsonAccessDeniedHandler accessDeniedHandler;
    final UserDetailsServiceImpl userDetailsService;
    final ValidateCodeFilter validateCodeFilter;
    final TokenFilter tokenFilter;

    @Autowired
    public SecurityConfig(JsonSuccessHandler successHandler, JsonFailureHandler failureHandler, JsonLogoutSuccessHandler logoutSuccessHandler, JsonAuthenticationEntryPoint authenticationEntryPoint, JsonAccessDeniedHandler accessDeniedHandler, UserDetailsServiceImpl userDetailsService, ValidateCodeFilter validateCodeFilter, TokenFilter tokenFilter) {
        this.successHandler = successHandler;
        this.failureHandler = failureHandler;
        this.logoutSuccessHandler = logoutSuccessHandler;
        this.authenticationEntryPoint = authenticationEntryPoint;
        this.accessDeniedHandler = accessDeniedHandler;
        this.userDetailsService = userDetailsService;
        this.validateCodeFilter = validateCodeFilter;
        this.tokenFilter = tokenFilter;
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterAfter(tokenFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .authorizeRequests()
                .antMatchers("/code", "/login", "/logout", "/doLogin").permitAll()
                .anyRequest().authenticated()
                .and().formLogin().loginProcessingUrl("/doLogin").successHandler(successHandler).failureHandler(failureHandler)
                .and().logout().logoutSuccessHandler(logoutSuccessHandler)
                .and().exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).accessDeniedHandler(accessDeniedHandler)
                .and().cors()
                .and().csrf().disable();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder());
    }
}
